package com.sojson.project.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.enums.EUserIsEnterpriseAuth;
import com.sojson.project.dao.MemberInfoDao;
import com.sojson.project.entity.po.MemberInfo;
import com.sojson.project.service.MemberInfoService;
import com.sojson.project.sys.user.dao.UserDao;
import com.sojson.project.sys.user.entity.po.User;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.depot.FileDepotUtil;
import com.sojson.util.file.enums.EFileTable;

/**
 * 企业资料认证表Service实现类
 * 
 * @author liu
 * @date 2021-03-16
 */
@Service
public class MemberInfoServiceImpl implements MemberInfoService {

    @Resource
    private MemberInfoDao memberInfoDaoImpl;
    @Resource
    private UserDao userDaoImpl;

    /**
     * 单条插入
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(MemberInfo memberInfo) {
        int insert = memberInfoDaoImpl.insert(memberInfo);
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(MemberInfo memberInfo) {
        int insertList = memberInfoDaoImpl.insertList(memberInfo.getMemberInfos());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(MemberInfo memberInfo) {
        int deleteByDto = memberInfoDaoImpl.deleteByDto(memberInfo);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = memberInfoDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<MemberInfo> findAll() {
        List<MemberInfo> findAll = memberInfoDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(MemberInfo memberInfo) {
        int countByDto = memberInfoDaoImpl.countByDto(memberInfo);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public List<MemberInfo> findByDto(MemberInfo memberInfo) {
        List<MemberInfo> findByDto = memberInfoDaoImpl.listByDto(memberInfo);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = memberInfoDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(ArrayList<Integer> ids) {
        int deleteByIdList = memberInfoDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(MemberInfo memberInfo) {
        int updateById = memberInfoDaoImpl.updateById(memberInfo);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(MemberInfo memberInfo) {
        int updateStaticByIdList = memberInfoDaoImpl.updateStaticByIdList(memberInfo);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(MemberInfo memberInfo) {
        int updateChangeByIdList = memberInfoDaoImpl.updateChangeByIdList(memberInfo.getMemberInfos());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(MemberInfo memberInfo) {
        int updatePartialById = memberInfoDaoImpl.updatePartialById(memberInfo);
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(MemberInfo memberInfo) {
        int updateStaticPartialByIdList = memberInfoDaoImpl.updateStaticPartialByIdList(memberInfo);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(MemberInfo memberInfo) {
        int updateChangePartialByIdList = memberInfoDaoImpl.updateChangePartialByIdList(memberInfo.getMemberInfos());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public MemberInfo findById(Integer id) {
        MemberInfo findById = memberInfoDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<MemberInfo> findByIdList(ArrayList<Integer> ids) {
        ArrayList<MemberInfo> findByIdList = memberInfoDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(MemberInfo memberInfo) {
        ArrayList<Integer> findIdByDto = memberInfoDaoImpl.findIdByDto(memberInfo);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        memberInfoDaoImpl.clear();
    }

    /**
     * 审核企业认证信息
     * 
     * @param memberInfo
     * @return
     * @throws SQLException 
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int audit(MemberInfo memberInfo) {
        String mid = memberInfo.getMid();
        // 参数初始化
        MemberInfo memInfo = new MemberInfo();
        memInfo.updateInit();
        memInfo.setMid(mid);
        memInfo.setStatus(memberInfo.getStatus());
        memInfo.setMark(memberInfo.getMark());
        int updatePartialById = memberInfoDaoImpl.updatePartialByMId(memInfo);

        User member = new User();
        member.setId(mid);
        member.setIsEnterpriseAuth(memberInfo.getStatus());
        userDaoImpl.updateById(member);
        return updatePartialById;
    }

    /**
     * 通过mId查询
     * 
     * @param mId
     * @return
     */
    @Override
    public MemberInfo findByMId(String mId) {
        MemberInfo findByMId = memberInfoDaoImpl.findByMId(mId);
        return findByMId;
    }

    /**
     * 通过mid更新(局部更新)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException 
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updatePartialByMId(MemberInfo memberInfo) {
        MemberInfo findById = memberInfoDaoImpl.findByMId(memberInfo.getMid());

        int updatePartialByMId = memberInfoDaoImpl.updatePartialByMId(memberInfo);

        // 将用户修改为未认证
        User member = new User();
        member.setId(memberInfo.getMid());
        member.setIsEnterpriseAuth(EUserIsEnterpriseAuth.NO.getCode());
        userDaoImpl.updateById(member);

        if (updatePartialByMId > 0) {
            FileDepotUtil.updateSync(EFileTable.MEMBER_INFO.getCode(), ConstantFileFieldName.BANK_CARD_IMG,
                memberInfo.getBankCardImg(), findById.getBankCardImg());
            FileDepotUtil.updateSync(EFileTable.MEMBER_INFO.getCode(), ConstantFileFieldName.BUSINESS_CERT_IMG,
                memberInfo.getBusinessCertImg(), findById.getBusinessCertImg());
        }
        return updatePartialByMId;
    }

}